<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;

/**
 * 翻译配置模型
 */
class TranslationConfig extends Model
{
    protected $table = 'translation_configs';

    protected function getOptions(): array
    {
        return [
            'type'               => [
                'target_languages' => 'json',
            ],
            'autoWriteTimestamp' => true,
            'jsonAssoc'          => true,
        ];
    }

    /**
     * 获取表的翻译配置
     */
    public static function getTableConfig($tableName)
    {
        return self::where('table_name', $tableName)
            ->where('is_enabled', 1)
            ->select();
    }

    /**
     * 获取字段的翻译配置
     */
    public static function getFieldConfig($tableName, $fieldName)
    {
        return self::where([
            'table_name' => $tableName,
            'field_name' => $fieldName,
            'is_enabled' => 1
        ])->find();
    }

    /**
     * 检查字段是否需要翻译
     */
    public static function shouldTranslate($tableName, $fieldName)
    {
        $config = self::getFieldConfig($tableName, $fieldName);
        return $config && $config->is_enabled;
    }

    /**
     * 获取字段的目标语言
     */
    public static function getTargetLanguages($tableName, $fieldName)
    {
        $config = self::getFieldConfig($tableName, $fieldName);
        if (!$config) {
            return [];
        }

        return $config->target_languages ?: Language::getEnabledLanguages()->column('code');
    }

    /**
     * 获取所有启用的翻译配置
     */
    public static function getEnabledConfigs()
    {
        return self::where('is_enabled', 1)->select();
    }

    /**
     * 批量创建翻译配置
     */
    public static function batchCreateConfigs($configs)
    {
        $data = [];
        foreach ($configs as $config) {
            $data[] = [
                'table_name'       => $config['table_name'],
                'field_name'       => $config['field_name'],
                'is_enabled'       => $config['is_enabled'] ?? 1,
                'auto_translate'   => $config['auto_translate'] ?? 1,
                'target_languages' => json_encode($config['target_languages'] ?? []),
                'created_at'       => date('Y-m-d H:i:s'),
                'updated_at'       => date('Y-m-d H:i:s'),
            ];
        }

        return self::insertAll($data);
    }

    /**
     * 获取可翻译的表列表
     */
    public static function getTranslatableTables()
    {
        return self::distinct(true)->column('table_name');
    }

    /**
     * 获取表的可翻译字段
     */
    public static function getTranslatableFields($tableName)
    {
        return self::where('table_name', $tableName)
            ->where('is_enabled', 1)
            ->column('field_name');
    }

    /**
     * 启用/禁用翻译配置
     */
    public function toggleEnabled()
    {
        return $this->save([
            'is_enabled' => $this->is_enabled ? 0 : 1
        ]);
    }

    /**
     * 启用/禁用自动翻译
     */
    public function toggleAutoTranslate()
    {
        return $this->save([
            'auto_translate' => $this->auto_translate ? 0 : 1
        ]);
    }

    /**
     * 获取配置统计
     */
    public static function getConfigStats()
    {
        return [
            'total_configs'          => self::count(),
            'enabled_configs'        => self::where('is_enabled', 1)->count(),
            'auto_translate_configs' => self::where('auto_translate', 1)->count(),
            'total_tables'           => self::distinct(true)->count('table_name'),
        ];
    }

    /**
     * 获取推荐的翻译配置
     */
    public static function getRecommendedConfigs()
    {
        return [
            // 商品相关
            ['table_name' => 'goods', 'field_name' => 'name', 'description' => '商品名称'],
            ['table_name' => 'goods', 'field_name' => 'content', 'description' => '商品详情'],
            ['table_name' => 'goods', 'field_name' => 'seo_title', 'description' => 'SEO标题'],
            ['table_name' => 'goods', 'field_name' => 'seo_description', 'description' => 'SEO描述'],

            // 分类相关
            ['table_name' => 'goods_category', 'field_name' => 'name', 'description' => '分类名称'],
            ['table_name' => 'goods_category', 'field_name' => 'description', 'description' => '分类描述'],

            // 文章相关
            ['table_name' => 'articles', 'field_name' => 'title', 'description' => '文章标题'],
            ['table_name' => 'articles', 'field_name' => 'content', 'description' => '文章内容'],

            // 系统配置
            ['table_name' => 'site_config', 'field_name' => 'site_name', 'description' => '网站名称'],
            ['table_name' => 'site_config', 'field_name' => 'site_description', 'description' => '网站描述'],
        ];
    }
}
